<template>
  <el-card shadow="never">
    <div slot="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"> <i class="el-icon-house"></i> 首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 主体内容 -->

    <!-- 表单 -->
    <el-form :model="params"
             ref="form"
             label-width="80px"
             :inline="true"
             size="small">
      <el-form-item label="用户ID"
                    prop="user_id">
        <el-input v-model="params.user_id"></el-input>
      </el-form-item>
      <el-form-item label="支付状态"
                    prop="pay_status">
        <el-select v-model="params.pay_status">
          <el-option label="全部状态"
                     value=""></el-option>
          <el-option label="已支付"
                     value="1"></el-option>
          <el-option label="未支付"
                     value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货状态"
                    prop="is_send">
        <el-select v-model="params.is_send">
          <el-option label="全部状态"
                     value=""></el-option>
          <el-option label="已发货"
                     value="1"></el-option>
          <el-option label="未发货"
                     value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="getOrdersList">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="list"
              border>
      <el-table-column label="#"
                       type="index"></el-table-column>
      <el-table-column label="订单编号"
                       width="300"
                       prop="order_number"></el-table-column>
      <el-table-column label="订单价格"
                       prop="order_price"></el-table-column>
      <el-table-column label="用户ID"
                       prop="user_id"></el-table-column>
      <el-table-column label="支付状态">
        <template slot-scope="scope">
          <el-tag type="success"
                  v-if="scope.row.pay_status==='1'">已付款</el-tag>
          <el-tag type="warning"
                  v-if="scope.row.pay_status==='0'">未付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否发货">
        <template slot-scope="scope">
          <el-tag type="success"
                  v-if="scope.row.is_send==='是'">是</el-tag>
          <el-tag type="warning"
                  v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单时间">
        <template slot-scope="scope">
          {{ scope.row.create_time | formate }}
        </template>
      </el-table-column>
      <el-table-column label="订单时间">
        <template slot-scope="scope">
          <el-button type="success"
                     size="small"
                     icon="el-icon-edit"
                     @click="isEditShow=true"></el-button>
          <el-button @click="seeExpress(scope.row)"
                     type="warning"
                     size="small"
                     icon="el-icon-truck"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination background
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   :page-size="params.pagesize"
                   :current-page="params.pagenum"
                   layout=' sizes,prev, pager, next,->, total'
                   :page-sizes="[10,20,30,40,50]"
                   :total="total">
    </el-pagination>

    <!-- 修改收货地址 -->
    <el-dialog title="修改收货地址"
               :visible.sync="isEditShow"
               width="30%">
      <!-- 表单 -->
      <el-form :model="addressForm"
               ref="form"
               :rules="addressRules"
               label-width="80px"
               :inline="false"
               size="normal">
        <el-form-item label="省市县"
                      prop="city">
          <el-cascader :options="cityOptions"
                       v-model="addressForm.city">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址"
                      prop="address">
          <el-input v-model="addressForm.address"
                    type="textarea"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer">
        <el-button @click="isEditShow = false">取消</el-button>
        <el-button type="primary">确定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息弹框 -->
    <el-dialog title="物流信息"
               :visible.sync="expressShow"
               width="50%"
               @close="expressList=[]">
      <!-- 时间线组件 -->
      <el-row>
        <el-col :span="20"
                :offset="2">
          <el-timeline>
            <el-timeline-item v-for="(item, index) in expressList"
                              :key="index"
                              :type="item.type"
                              :icon="item.icon"
                              :timestamp="item.ftime">
              {{item.context}}
            </el-timeline-item>
          </el-timeline>
        </el-col>
      </el-row>
      <span slot="footer">
        <el-button type="primary"
                   @click="expressShow=false">关闭</el-button>
      </span>
    </el-dialog>

  </el-card>

</template>

<script>
// 导入省市县数据
import cityOptions from '@/libs/city_data2017_element.js'
// https://github.com/iceyangcc/provinces-china
export default {
  name:'Orders',
  data () {
    return {
      params:{
        user_id:'',
        pagenum:1,
        pagesize:10,
        pay_status:'',
        is_send:''
      },
      list:[],
      total:0,
      isEditShow:false,
      addressForm:{
        city:[],
        address:''
      },
      addressRules:{
        city:[{ required:true, message:'请选择地区', trigger:'blur' }],
        address:[{ required:true, message:'请输入详细地址', trigger:'blur' }]
      },
      cityOptions,
      expressList:[], // 订单物流信息
      expressShow:false
    }
  },
  created () {
    this.getOrdersList()
  },
  methods: {
    // 订单列表
    async getOrdersList () {
      const { data:res } = await this.$axios.get('/orders', {
        params:this.params
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.list = res.data.goods
      this.total = res.data.total
    },
    // 页码变化
    currentChange (val) { // val是最新页码
      this.params.pagenum = val
      this.getOrdersList()
    },
    // 每页数量变化
    sizeChange (val) { // val是最新的每页数量
      this.params.pagesize = val
      this.getOrdersList()
    },
    // 搜索重置
    reset () {
      this.$refs.form.resetFields()
      this.params.pagenum = 1
      this.getOrdersList()
    },
    // 查看物流信息
    async seeExpress (val) {
      if (val.is_send !== '是') {
        return this.$notify.warning({
          title:'注意',
          message:'订单没发货，暂无物流信息'
        })
      }
      // console.log(val.order_id)
      // const { data:res } = await this.$axios.get('/kuaidi/' + val.order_id)
      // const { data:res } = await this.$axios.get('/kuaidi/1106975712662')
      const res = {
        data: [
          {
            time: '2018-05-10 09:39:00',
            ftime: '2018-05-10 09:39:00',
            context: '已签收,感谢使用顺丰,期待再次为您服务',
            location: ''
          },
          {
            time: '2018-05-10 08:23:00',
            ftime: '2018-05-10 08:23:00',
            context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
            location: ''
          },
          {
            time: '2018-05-10 07:32:00',
            ftime: '2018-05-10 07:32:00',
            context: '快件到达 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-10 02:03:00',
            ftime: '2018-05-10 02:03:00',
            context: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-09 23:05:00',
            ftime: '2018-05-09 23:05:00',
            context: '快件到达 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 21:21:00',
            ftime: '2018-05-09 21:21:00',
            context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 13:07:00',
            ftime: '2018-05-09 13:07:00',
            context: '顺丰速运 已收取快件',
            location: ''
          },
          {
            time: '2018-05-09 12:25:03',
            ftime: '2018-05-09 12:25:03',
            context: '卖家发货',
            location: ''
          },
          {
            time: '2018-05-09 12:22:24',
            ftime: '2018-05-09 12:22:24',
            context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
            location: ''
          },
          {
            time: '2018-05-08 21:36:04',
            ftime: '2018-05-08 21:36:04',
            context: '商品已经下单',
            location: ''
          }
        ],
        meta: { status: 200, message: '获取物流信息成功！' }
      }
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      res.data[0].type = 'success'
      res.data[0].icon = 'el-icon-position'
      this.expressList = res.data
      this.expressShow = true
    }
  }
}
</script>

<style>

</style>
